package com.biscuit.page.basicservice.websocket.ai_chat;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.biscuit.page.basicservice.design.websocket.register.AiChatSessionWebSocketRegisterContext;
import com.biscuit.page.basicservice.design.websocket.register.WebSocketRegisterTypeEnum;
import com.biscuit.page.basicservice.design.websocket.register.factory.WebSocketRegisterHandlerFactory;
import com.biscuit.page.basicservice.utils.BaseExceptionUtil;
import com.biscuit.page.basicservice.utils.BaseVerifyUtil;
import com.biscuit.page.basicservice.websocket.MessageCallbackTypeEnum;
import com.biscuit.page.basicservice.websocket.chat_room.ChatRoomWebSocketContext;
import com.biscuit.page.basicservice.websocket.chat_room.ChatRoomWebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.Objects;

/**
 * @author biscuit
 * @create 2024年08月01日 9:23
 */
@Slf4j
@Component
@ServerEndpoint("/websocket/ai/chat/{sessionId}")
public class AiChatSessionWebSocketServer {

    @OnOpen
    public void connect(Session session,
                        @PathParam("sessionId") Long sessionId) {
        // 参数校验
        BaseExceptionUtil.throwException(BaseVerifyUtil.verifyNumberFullValid(sessionId), "非法会话ID");

        // 调用对应的 WebSocket 注册策略，进行 WebSocket 的注册
        AiChatSessionWebSocketRegisterContext context = new AiChatSessionWebSocketRegisterContext();
        context.setSession(session);

        // 获取聊天室 WebSocket 连接注册实现类，并执行注册逻辑
        WebSocketRegisterHandlerFactory.create(WebSocketRegisterTypeEnum.CHAT_ROOM_REGISTER.getType())
                .register(context);
    }

    /**
     * 客户端与服务器双向通信连接断开后的回调方法
     */
    @OnClose
    public void disconnect(Session session) {
    }

    /**
     * 收到客户端消息后调用的方法
     */
    @OnMessage
    public void message(String message, Session session) {
    }

    /**
     * 客户端与服务器双向通信连接发生错误时回调方法
     */
    @OnError
    public void error(Session session, Throwable error) {
    }

}
